package com.lhy.mymall.common;

import com.lhy.mymall.exception.ExceptionEnum;
import com.lhy.mymall.exception.MallException;
import io.lettuce.core.internal.LettuceSets;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * @author: 卢辉宇
 * @date: 2022/12/14 13:03
 * @description: 常量类，用于封装各种常量
 */
@Component
public class Constant {
    //用户登录的redis存验证码key的前缀
    public static final String LOGIN_CODE_PRE="login:code:";

    //用户登录后的session的key
    public static final String MALL_USER="mallUser";

    //发件人,配置文件中的发件地址
    public static final String MAIL_FROM="luhuiyu2020@qq.com";
    //邮件主题
    public static final String MAIL_SUBJECT="您的验证码";

    //JWT的签名密钥
    public static final String JWT_KEY = "crazy-mall";
    //jwt的信息体的key
    public static final String USER_NAME = "user_name";
    public static final String USER_ID = "user_id";
    public static final String USER_ROLE = "user_role";
    //JWT的过期时间：2天（毫秒）
    public static final long EXPIRE_TIME = 1000*60*60*24*2L;
    public static final String JWT_TOKEN = "jwt_token";

    //上传文件夹地址,从配置文件中注入,由于是静态的，所以要用set方法赋值
    public static String FILE_UPLOAD_DIR;

    //保存二维码的文件夹地址,从配置文件中注入,由于是静态的，所以要用set方法赋值
    public static String FILE_QRCODE_DIR;


    //发注册验证码缓存到redis的地址
    public static String REDIS_HOST;
    public static String REDIS_PORT;
    public static String REDIS_PASSWORD;

    @Value("${file.upload.dir}")
    public void setFileUploadDir(String dir){
        Constant.FILE_UPLOAD_DIR=dir;
    }

    @Value("${file.qrcode.dir}")
    public void setFileQrcodeDirDir(String dir){
        Constant.FILE_QRCODE_DIR=dir;
    }

    @Value("${spring.redis.host}")
    public void setRedisHost(String redisHost) {
        REDIS_HOST = redisHost;
    }

    @Value("${spring.redis.port}")
    public void setRedisPort(String redisPort) {
        REDIS_PORT = redisPort;
    }
    @Value("${spring.redis.password}")
    public void setRedisPassword(String redisPassword) {
        REDIS_PASSWORD = redisPassword;
    }

    //支持的商品排序规则
    public static final Set<String> PRICE_ASC_DESC= LettuceSets.newHashSet("price asc", "price desc");

    //定义商品的上下架状态
    public interface SaleStatus{
        int SALE=1; //商品上架
        int NOT_SALE=0; //商品下架
    }

    //定义购物车的选择状态
    public interface Cart{
        int UN_CHECKED=0; //商品没选中
        int CHECKED=1; //商品选中
    }

    //定义订单状态的枚举，订单状态: 0用户已取消，10未付款（初始状态），20已付款，30已发货，40交易完成
    public enum OrderStatusEnum{
        CANCELED(0,"用户已取消"),
        NOT_PAID(10,"未付款"),
        PAID(20,"已付款"),
        DELIVERED(30,"已发货"),
        FINISHED(40,"交易完成");

        private Integer code;
        private String msg;

        /**
         * 通过枚举码得到对应的枚举
         * @param code 枚举码
         * @return 订单状态枚举
         * @throws MallException
         */
        public static OrderStatusEnum codeOf(Integer code) throws MallException {
            //遍历此枚举是否包含传进来的code
            for(OrderStatusEnum orderStatusEnum:values()){
                if(orderStatusEnum.getCode()==code){
                    return orderStatusEnum;
                }
            }
            throw new MallException(ExceptionEnum.NOT_ENUM);
        }

        OrderStatusEnum(Integer code, String msg) {
            this.code = code;
            this.msg = msg;
        }

        public Integer getCode() {
            return code;
        }

        public void setCode(Integer code) {
            this.code = code;
        }

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }
    }
}
